<template>
    <div class="container addmusic loading">
        <main  class="table-div">
            <el-form ref="ruleForm" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm">
              <el-form-item label="角色名称">
                <el-input readonly v-model="rolename"></el-input>
              </el-form-item>
               <el-form-item label="用户列表">
                <el-select
                  v-model="checkedUid"
                  multiple
                  filterable
                  reserve-keyword
                  placeholder="可输入用户名搜索"
                  remote
                  :loading="loading"
                  :remote-method="remoteMethod">
                  <el-option
                    v-for="item in allOptionData"
                    :key="item.id"
                    :label="item.realname ? item.realname: item.userid"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button  type="primary" @click="submitAllot('ruleForm')">绑定用户</el-button>
              </el-form-item>
            </el-form>
        </main>
    </div>
</template>

<script>
import { userToRole, getUserByRole } from '@/api/role'
import { getUserList } from '@/api/user'
export default {
  data () {
    return {
      id: '',
      haha: '',
      rolename: '',
      loading: false,
      form: {
        rid: '',
        ids: ''
      },
      rules: {

      },
      userAjax: {
        pageNo: 1,
        pageSize: 20,
        findContent: ''
      },
      optionData: [],
      checkedOptionData: [],
      checkedUid: []
    }
  },
  components: {
  },
  computed: {
    allOptionData () {
      var [...darr] = this.optionData.filter(v => {
        if (!this.checkedUid.includes(v.id)) {
          return v
        }
      })
      return [...darr, ...this.checkedOptionData]
    }
  },
  methods: {
    getUserByRid (id) {
      getUserByRole({ rid: id }).then((data) => {
        if (data.result === 1) {
          [...this.checkedUid] = data.data.map(item => {
            return item.id
          })
          this.checkedOptionData = data.data
        }
      })
    },
    getUserData () {
      getUserList(this.userAjax).then(data => {
        this.loading = false
        if (data.result === 1) {
          this.optionData = data.data.list
        } else {
          this.optionData = []
        }
      })
    },
    remoteMethod (value) {
      this.userAjax.findContent = value
      if (!this.loading) {
        this.loading = true
        setTimeout(() => {
          this.getUserData()
        }, 1000)
      }
    },
    submitAllot (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.form.rid = this.id
          this.form.ids = this.checkedUid.join(',')
          userToRole(this.form).then((data) => {
            if (data.result === 1) {
              setTimeout(() => {
                this.$router.replace({ name: 'RoleList', params: { page: this.oldPage }})
              }, 1000)
            }
          })
        } else {
          return false
        }
      })
    }
  },
  created () {
    this.id = this.$route.params.id
    this.rolename = this.$route.params.rolename
    this.oldPage = this.$route.params.page ? this.$route.params.page : ''
  },
  beforeMount () {
    this.getUserData()
    this.getUserByRid(this.id)
  },
  mounted () {

  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.table-div{
    width:90%;
    margin: 20px auto
}
.title{
    width:90%;
    margin: 10px auto;
    .search-div,.row-div{
      float: left;
    }
    .search-div{
      width: 200px;
      margin-right: 20px
    }
}
.el-upload-list__item{
  height:auto !important;
}
</style>
